package com.hue.ps.topic;

import com.hue.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
    发布与订阅模式之topic：消费者2
 */
public class Customer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //2.获取连接
        Connection connection = ConnectionUtils.getConnection();
        //3.获取频道
        Channel channel = connection.createChannel();

        //4.声明队列
        channel.queueDeclare("topic_queue2", true, false, false, null);

        //5.创建消费者来处理消息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("customer2接收到的消息："+new String(body));
            }
        };

        //6.监听队列
        channel.basicConsume("topic_queue2", true,consumer);
    }
}
